package com.test31_IO应用;

import com.test31_IO应用.exception.MaxTryCountException;
import com.test31_IO应用.util.InputUtil;

interface ILoginService {
    public boolean login();
}

class LoginServiceImpl implements ILoginService {
    private String uname;
    private String upass;

    public LoginServiceImpl(String uname, String upass) {
        this.uname = uname;
        this.upass = upass;
    }

    @Override
    public boolean login() {
        if ("yootk".equals(this.uname) && "muyan".equals(this.upass)) {
            return true;
        }
        return false;
    }
}

class LoginFactory {
    private LoginFactory() {
    }

    public static ILoginService getInstance(String uname, String upass) {
        return new LoginServiceImpl(uname, upass);
    }
}

class LoginMain {
    private String[] args;  // 外部的接收参数
    private String userName;
    private String userPassword;
    private int count;  // 尝试次数

    public LoginMain(String[] args) {
        this.args = args;
        while (true) {
            if (this.count > 3) {
                throw new MaxTryCountException("用户登录尝试次数过多，为了安全起见，系统退出！");
            }
            this.handle();
            ILoginService loginService = LoginFactory.getInstance(this.userName, this.userPassword);
            if (loginService.login()) {
                System.out.println("【成功】欢迎“YOOTK“用户登录，请访问：www.yootk.com。");
                break;
            } else {
                System.out.println("【失败】错误的用户名或密码，请确认你的登录信息。");
                count++;
            }
        }
    }

    private void handle() {
        if (this.args.length == 0) {
            this.userName = InputUtil.getString("请输入登录用户名：");
            this.userPassword = InputUtil.getString("请输入登录密码：");
        } else if (this.args.length == 1) {
            this.userName = this.args[0];
            this.userPassword = InputUtil.getString("请输入登录密码：");
        } else if (this.args.length == 2) {
            this.userName = this.args[0];
            this.userPassword = this.args[1];
        }
    }
}

/**
 * 用户登录认证
 */
public class YootkDemo_04 {

    public static void main(String[] args) {
        new LoginMain(args);
    }
}
